function list(pageNo,whereValue)
{
	var url = getAction()+"?cmd=data&"+$("#deptForm").formSerialize();
	
	$.ajax({
		type: "POST",
		url: url,
		dataType: "json",
		success: function(returnedData){
			$("#dataList").get(0).innerHTML=returnedData;
		},error:function(returnedData){
			$("#dataList").get(0).innerHTML=returnedData.responseText;
		}
	});
}


function getForm(){
return $("#userForm");
}

function getAction(){
	return "users.action";
}

/*
 * 跳转到添加页面
 */
function goAdd(){
	var retValue = window.showModalDialog(getAction()+"?cmd=goAdd&po.deptId="+$$("po.deptId").value,window,"menubar=no;toolbar=no");
}

/*
 * 添加操作，然后返回list页面
 */
function add(){
	if(!validateForm())
	{
		return;
	}
	var url=getAction()+"?"+$("#userForm").formSerialize();
	
	saveOrUpdate(url);
}

/**
 * 跳转到更新页面
 */
function goUpdate(id)
{
	window.showModalDialog(getAction()+"?cmd=goUpdate&po.deptId="+$$("po.deptId").value+"&po.id="+id,window,"menubar=no;toolbar=no");
}
//重置密码
function resetpwd(userName){
//	var url ="resetpwd.action?cmd=update&userName="+userName;
//	new Ajax.Request(url,{method: "post", onSuccess: function(transport) {
//    	//var json = transport.responseText;
//    	//document.all("lgr").innerHTML=json;
//    	//alert(document.all("pass").value);
//    	var json = transport.responseText.strip().replace(/&quot;/g, '"').evalJSON();
//    	alert(json.msg);
//  	}});
	window.open("resetpwd.action?cmd=update&userName="+userName,null,"height=400,width=400,status=yes,toolbar=no,menubar=no,location=no");
//	window.location="resetpwd.action?cmd=update&userName="+userName;
}

//账单组配置
function goBillGroup(userName,userId){
	window.showModalDialog("userBillGroup.action?cmd=list&userName="+userName+"&po.userId="+userId,null,"height=400,width=400,status=yes,toolbar=no,menubar=no,location=no");
}

/**
 * 导出
 */
function exportPart(){
//	new Ajax.Request("admsMove.action",{method: "post", onSuccess: function(transport) {
//    	var json = transport.responseText.strip().replace(/&quot;/g,'"').evalJSON();
//    	alert(json.msg);
//  	}});
	var tempAction=$("deptForm").action;
	$("deptForm").action="admsMove.action";
	$("deptForm").submit();
	$("deptForm").action=tempAction;
}
/**
 * 更新一条记录
 */
function update()
{
	if(!validateForm())
	{
		return;
	}
	var url = getAction()+"?"+getForm().formSerialize();
	saveOrUpdate(url);
}

//密码长度及合法性校验
function validatePwd()
{   //var passwordminLength=$("passwordminLength").value;
    //var passwordmaxLength=$("passwordmaxLength").value;
    var success = true;
    if(!checkBlank($("po.password"),"新密码"))
    {
        success = false;
    }else if(!legalValidate($("po.password").value))
    {
        success = false;
    }
    return success;
}

//用户密码必须同时包含大写字母、小写字母、数字和特殊字符四类要素中的三种，且不得与用户账号相同。
function legalValidate(str){
	
	var passwordminLength=$$("passwordminLength").value;
    var passwordmaxLength=$$("passwordmaxLength").value;
    var lengthflag=false;
    if($$("po.password").value.length<passwordminLength || $$("po.password").value.length>passwordmaxLength){
        lengthflag=true;
    }
	
    var value = true;
    var item=0;
    var rule1=/[0-9]/;
    var rule2=/[a-z]/;
    var rule3=/[A-Z]/;
    var rule4=/\W|_/;
    if(rule1.test(str))
       item=item+1;
    if(rule2.test(str))
       item=item+1;
    if(rule3.test(str))
       item=item+1;
    if(rule4.test(str))
        item=item+1;
    if(item<3 || lengthflag){
        value=false;
        alert("您的新密码字段必须长度必须大于等于"+passwordminLength+",小于等于"+passwordmaxLength+",且同时包含大写字母、小写字母、数字和特殊字符四类要素中的三种！");
    }else if(str==$$("po.userName").value)
    {
       value=false;
       alert("密码不能与用户名相同！");
    }
    return value;
}

/**
 * 修改密码
 */
function modifyPW()
{
	var old = $$("old").value;
	var oldPW = $$("oldPW").value;
	var pw = $$("password").value;
	var repw = $$("repassword").value;
	if(old!=oldPW)
	{
		alert("原始密码不正确！");
		return;
	}else if(!validatePwd()){
		return;
	}else if(!(pw==repw))
	{
		alert("两次输入的密码不一致！");
		return;
	}
	
	var url ="users.action?cmd=update&po.userId="+$$("po.userId").value+"&po.deptId="+$$("po.deptId").value+"&po.password="+$$("po.password").value;
	$.ajax({
		type: "POST",
		url: url,
		dataType: "json",
		success: function(returnedData){
			alert(json.msg);
		}
	});
}

/**
 * 添加后返回添加页面继续添加一个记录
 */
function addNext()
{
	if(!validateForm())
	{
		return;
	}
	var url = getAction()+"?"+getForm().formSerialize();
	saveOrUpdate(url,"addNext");
}
/*
 * 删除选择的记录
 */
function del()
{
	if(isSelect()==0){
		alert("请选择需要删除的记录！");
		return;
	}
	if(confirm("您确定要删除这些数据么？")==false)
		return;
	$("deptForm").request({
		parameters: { cmd:'delete'},
		onSuccess:function(transport){
			var retObj = transport.responseText.strip().replace(/&quot;/g, '"').evalJSON();
			alert(retObj.msg);
			if(retObj.processId=='1'){
				list(1);
			}
	}});
}

/**
 * 表单域校验
 */
function validateForm()
{
	var success = true;
	if(!checkBlank($$("po.userName"),"登陆名称")||!checkCharNumber($$("po.userName"),"登陆名称")||!MaxLength($$("po.userName"),"登陆名称",12)||!MinLength($$("po.userName"),"登陆名称",7))
	{
		success = false;
	}
	else if(!checkBlank($$("po.userCname"),"中文名称")||!MaxLength($$("po.userCname"),"中文名称",30))
	{
		success = false;
	}
	else if(!MaxLength($$("fullName"),"全名",30))
	{
		success = false;
	}
	else if(!checkTelephone($$("telephone"),"电话"))
	{
		success = false;
	}
	else if(!checkMail($$("userEmail")))
	{
		success = false;
	}
	/*
    */
	var rule1=/[a-zA-Z]/;
	var rule2=/[0-9]/;　
    if(!(rule1.test($$("po.userName").value)&& rule2.test($$("po.userName").value))){
        success = false;
        alert("登陆名称必须由数字和英文字母组成！");
    }
	return success;
}

function saveOrUpdate(url,type){
	$.ajax({
		type: "POST",
		url: url,
		dataType:"html",
		success: function(returnedData){
			returnedData = $.evalJSON(returnedData);
			alert(returnedData.msg);
			if(returnedData.processId=='1'){
				var parentWindow = window.dialogArguments;
				parentWindow.list(1);
			}
			if(type !== 'addNext' ){	//未定义
				window.close();
				getForm().reset();		//重置
			}
	
		}
	});
	
}
